/*-----------------------------------------------------------------------------
HS52F003 Function_define.h V1.0

All function define inital setting file for Huaxin Micro-electronics HS52F003
------------------------------------------------------------------------------*/

#ifndef __FUNCTION_DEFINE_H
#define __FUNCTION_DEFINE_H

#include <intrins.h>
#include <stdio.h>

#define nop _nop_();


//16 --> 8 x 2
#define HIBYTE(v1)              ((UINT8)((v1)>>8))                      //v1 is UINT16
#define LOBYTE(v1)              ((UINT8)((v1)&0xFF))
//8 x 2 --> 16
#define MAKEWORD(v1,v2)         ((((UINT16)(v1))<<8)+(UINT16)(v2))      //v1,v2 is UINT8
//8 x 4 --> 32
#define MAKELONG(v1,v2,v3,v4)   (UINT32)((v1<<32)+(v2<<16)+(v3<<8)+v4)  //v1,v2,v3,v4 is UINT8
//32 --> 16 x 2
#define YBYTE1(v1)              ((UINT16)((v1)>>16))                    //v1 is UINT32
#define YBYTE0(v1)              ((UINT16)((v1)&0xFFFF))
//32 --> 8 x 4
#define TBYTE3(v1)              ((UINT8)((v1)>>24))                     //v1 is UINT32
#define TBYTE2(v1)              ((UINT8)((v1)>>16))
#define TBYTE1(v1)              ((UINT8)((v1)>>8)) 
#define TBYTE0(v1)              ((UINT8)((v1)&0xFF))

#define SET_BIT0        0x01
#define SET_BIT1        0x02
#define SET_BIT2        0x04
#define SET_BIT3        0x08
#define SET_BIT4        0x10
#define SET_BIT5        0x20
#define SET_BIT6        0x40
#define SET_BIT7        0x80
#define SET_BIT8        0x0100
#define SET_BIT9        0x0200
#define SET_BIT10       0x0400
#define SET_BIT11       0x0800
#define SET_BIT12       0x1000
#define SET_BIT13       0x2000
#define SET_BIT14       0x4000
#define SET_BIT15       0x8000

#define CLR_BIT0        0xFE
#define CLR_BIT1        0xFD
#define CLR_BIT2        0xFB
#define CLR_BIT3        0xF7
#define CLR_BIT4        0xEF
#define CLR_BIT5        0xDF
#define CLR_BIT6        0xBF
#define CLR_BIT7        0x7F

#define CLR_BIT8        0xFEFF
#define CLR_BIT9        0xFDFF
#define CLR_BIT10       0xFBFF
#define CLR_BIT11       0xF7FF
#define CLR_BIT12       0xEFFF
#define CLR_BIT13       0xDFFF
#define CLR_BIT14       0xBFFF
#define CLR_BIT15       0x7FFF

#define FAIL            1
#define PASS            0

/*****************************************************************************************
* For GPIO INIT setting 
*****************************************************************************************/
//------------------- Define Port as Push Pull mode -------------------
#define P00_PushPull_Mode			P0M |= SET_BIT0
#define P01_PushPull_Mode			P0M |= SET_BIT1
#define P10_PushPull_Mode			P1M |= SET_BIT0
#define P11_PushPull_Mode			P1M |= SET_BIT1
#define P12_PushPull_Mode			P1M |= SET_BIT2
#define P13_PushPull_Mode			P1M |= SET_BIT3
#define P14_PushPull_Mode			P1M |= SET_BIT4
#define P15_PushPull_Mode			P1M |= SET_BIT5
#define P16_PushPull_Mode			P1M |= SET_BIT6
#define P17_PushPull_Mode			P1M |= SET_BIT7
#define P20_PushPull_Mode			P2M |= SET_BIT0
#define P21_PushPull_Mode			P2M |= SET_BIT1
#define P22_PushPull_Mode			P2M |= SET_BIT2
#define P23_PushPull_Mode			P2M |= SET_BIT3
#define P24_PushPull_Mode			P2M |= SET_BIT4
#define P25_PushPull_Mode			P2M |= SET_BIT5
#define P26_PushPull_Mode			P2M |= SET_BIT6
#define P27_PushPull_Mode			P2M |= SET_BIT7


#define P00_Input_Mode			P0M &= ~SET_BIT0
#define P01_Input_Mode			P0M &= ~SET_BIT1
#define P10_Input_Mode			P1M &= ~SET_BIT0
#define P11_Input_Mode			P1M &= ~SET_BIT1
#define P12_Input_Mode			P1M &= ~SET_BIT2
#define P13_Input_Mode			P1M &= ~SET_BIT3
#define P14_Input_Mode			P1M &= ~SET_BIT4
#define P15_Input_Mode			P1M &= ~SET_BIT5
#define P16_Input_Mode			P1M &= ~SET_BIT6
#define P17_Input_Mode			P1M &= ~SET_BIT7
#define P20_Input_Mode			P2M &= ~SET_BIT0
#define P21_Input_Mode			P2M &= ~SET_BIT1
#define P22_Input_Mode			P2M &= ~SET_BIT2
#define P23_Input_Mode			P2M &= ~SET_BIT3
#define P24_Input_Mode			P2M &= ~SET_BIT4
#define P25_Input_Mode			P2M &= ~SET_BIT5
#define P26_Input_Mode			P2M &= ~SET_BIT6
#define P27_Input_Mode			P2M &= ~SET_BIT7


#define P00_Pullup_Mode			P0PH |= SET_BIT0
#define P01_Pullup_Mode			P0PH |= SET_BIT1
#define P10_Pullup_Mode			P1PH |= SET_BIT0
#define P11_Pullup_Mode			P1PH |= SET_BIT1
#define P12_Pullup_Mode			P1PH |= SET_BIT2
#define P13_Pullup_Mode			P1PH |= SET_BIT3
#define P14_Pullup_Mode			P1PH |= SET_BIT4
#define P15_Pullup_Mode			P1PH |= SET_BIT5
#define P16_Pullup_Mode			P1PH |= SET_BIT6
#define P17_Pullup_Mode			P1PH |= SET_BIT7
#define P20_Pullup_Mode			P2PH |= SET_BIT0
#define P21_Pullup_Mode			P2PH |= SET_BIT1
#define P22_Pullup_Mode			P2PH |= SET_BIT2
#define P23_Pullup_Mode			P2PH |= SET_BIT3
#define P24_Pullup_Mode			P2PH |= SET_BIT4
#define P25_Pullup_Mode			P2PH |= SET_BIT5
#define P26_Pullup_Mode			P2PH |= SET_BIT6
#define P27_Pullup_Mode			P2PH |= SET_BIT7



#define Set_All_PushPull_Mode			P0M=0xFF;P1M=0xFF;P2M=0xFF


/*****************************************************************************************
* For TIMER VALUE setting is base on " option -> C51 -> Preprocesser Symbols -> Define "
*****************************************************************************************/

//#ifdef FOSC_160000		// if Fsys = 16MHz 
		#define TIMER_DIV12_VALUE_10us			65536-13		//13*12/16000000 = 10 uS,  			// Timer divider = 12 for TM0/TM1
		#define TIMER_DIV12_VALUE_100us			65536-130		//130*12/16000000 = 10 uS,  		// Timer divider = 12 
		#define TIMER_DIV12_VALUE_1ms				65536-1334	//1334*12/16000000 = 1 mS, 			// Timer divider = 12 
		#define TIMER_DIV12_VALUE_10ms			65536-13334	//13334*12/16000000 = 10 mS 		// Timer divider = 12 
		#define TIMER_DIV12_VALUE_40ms			65536-53336	//53336*12/16000000 = 40 ms			// Timer divider = 12 
		#define TIMER_DIV4_VALUE_10us				65536-40		//40*4/16000000 = 10 uS,    		// Timer divider = 4	for TM2/TM3
		#define TIMER_DIV4_VALUE_100us			65536-400		//400*4/16000000 = 100 us				// Timer divider = 4
		#define TIMER_DIV4_VALUE_200us			65536-800		//800*4/16000000 = 200 us				// Timer divider = 4
		#define TIMER_DIV4_VALUE_500us			65536-2000	//2000*4/16000000 = 500 us			// Timer divider = 4
    #define TIMER_DIV4_VALUE_1ms				65536-4000	//4000*4/16000000 = 1 mS,   		// Timer divider = 4
		#define TIMER_DIV16_VALUE_10ms			65536-10000	//10000*16/16000000 = 10 ms			// Timer	divider = 16
		#define TIMER_DIV64_VALUE_30ms			65536-7500	//7500*64/16000000 = 30 ms			// Timer divider = 64
		#define	TIMER_DIV128_VALUE_100ms		65536-12500	//12500*128/16000000 = 100 ms		// Timer divider = 128
		#define	TIMER_DIV128_VALUE_200ms		65536-25000	//25000*128/16000000 = 200 ms		// Timer divider = 128
		#define TIMER_DIV256_VALUE_500ms		65536-31250	//31250*256/16000000 = 500 ms 	// Timer divider = 256
		#define	TIMER_DIV512_VALUE_1s				65536-31250	//31250*512/16000000 = 1 s.  		// Timer Divider = 512
//#endif


//-------------------- Timer0 function define --------------------
#define		TIMER1_MODE0_ENABLE		TMOD&=0x0F
#define		TIMER1_MODE1_ENABLE		TMOD&=0x0F;TMOD|=0x10
#define		TIMER1_MODE2_ENABLE		TMOD&=0x0F;TMOD|=0x20
#define		TIMER1_MODE3_ENABLE		TMOD&=0x0F;TMOD|=0x30
//-------------------- Timer1 function define --------------------
#define		TIMER0_MODE0_ENABLE		TMOD&=0xF0
#define		TIMER0_MODE1_ENABLE		TMOD&=0xF0;TMOD|=0x01
#define		TIMER0_MODE2_ENABLE		TMOD&=0xF0;TMOD|=0x02
#define		TIMER0_MODE3_ENABLE		TMOD&=0xF0;TMOD|=0x03








#endif